using UnityEngine;

namespace Weapon
{
    public class ProjectileBullet : MonoBehaviour
    {
        public float attackRange;
        private GameObject _effect; // 当前子弹的击中特效
        private GameObject burnArea;//子弹掉落到地上的效果，会让玩家受到伤害
        
        //实现抛物线运动属性
        public Transform target; // 目标位置
        public float launchAngle = 45f; // 发射角度（默认45度）
        public float gravity = -9.81f; // 重力加速度

        private Vector3 _startPos;// 子弹初始位置
        private Vector3 _targetPos;// 目标位置
        private float _initialSpeed;// 初速度
        private float _timeToTarget;// 到达目标的时间
        private Vector3 _initialVelocity;// 初速度向量
        private float _startTime;// 起始时间
        
        private void Awake()
        {
            _effect = Resources.Load<GameObject>("Prefabs/Effect/BulletHitEffect");
            burnArea = Resources.Load<GameObject>("Prefabs/Enemy/UI/BurnArea");
        }

        void OnEnable()
        {
            target = Player.Player.instance.transform;
            _startPos = transform.position;
            _targetPos = target.position;

            CalculateTrajectory();
            _startTime = Time.time;
        }

        void Update()
        {
            float t = Time.time - _startTime;

            if (t <= _timeToTarget)
            {
                // 计算当前位置
                Vector3 position = _startPos + _initialVelocity * t + new Vector3(0, gravity, 0) * (0.5f * t * t);
                transform.position = position;
            }
            else
            {
                transform.position = _targetPos;
                gameObject.SetActive(false);
            }
        }

        /// <summary>
        /// 计算抛物线运动属性
        /// </summary>
        void CalculateTrajectory()
        {
            // 计算水平方向向量（XZ平面）
            Vector3 horizontalDirection = (new Vector3(_targetPos.x, 0, _targetPos.z) - new Vector3(_startPos.x, 0, _startPos.z)).normalized;

            // 计算水平距离和高度差
            float horizontalDistance = Vector3.Distance(new Vector3(_startPos.x, 0, _startPos.z), new Vector3(_targetPos.x, 0, _targetPos.z));
            float verticalDifference = _targetPos.y - _startPos.y;

            // 将角度转换为弧度
            float radian = launchAngle * Mathf.Deg2Rad;

            // 计算初速度（物理公式）
            _initialSpeed = Mathf.Sqrt(
                (horizontalDistance * horizontalDistance * Mathf.Abs(gravity)) /
                (2 * (horizontalDistance * Mathf.Sin(radian) * Mathf.Cos(radian) - verticalDifference * Mathf.Pow(Mathf.Cos(radian), 2)))
            );

            // 分解初速度到水平和垂直方向
            Vector3 horizontalVelocity = horizontalDirection * _initialSpeed * Mathf.Cos(radian);
            float verticalVelocity = _initialSpeed * Mathf.Sin(radian);

            // 合成初速度向量
            _initialVelocity = horizontalVelocity + Vector3.up * verticalVelocity;

            // 计算到达时间（基于水平速度）
            _timeToTarget = horizontalDistance / (_initialSpeed * Mathf.Cos(radian));
            
        }
        
        private void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.CompareTag("Player"))
            {
                // 播放音效
                //Player.Player.instance.GetComponent<Player.Player>().hp -= attackRange;
                Player.Player.instance.HitPlayer(attackRange);
                // 生成粒子特效
                PoolManager.Release(_effect, transform.position);
                Debug.Log("Hit the player!");
                // 生成火焰效果
                PoolManager.Release(burnArea, new Vector3(_targetPos.x, burnArea.transform.position.y, _targetPos.z));
                gameObject.SetActive(false);
            }else if (other.gameObject.CompareTag("Ground"))
            {
                // 播放音效
                // 生成粒子特效
                PoolManager.Release(_effect, transform.position);
                Debug.Log("Hit the ground!");

                // 生成火焰效果
                PoolManager.Release(burnArea, new Vector3(_targetPos.x, burnArea.transform.position.y, _targetPos.z));
                gameObject.SetActive(false);
            }

        }
    }
}
